package com.twu.biblioteca;

import java.util.ArrayList;

public class Record {
    ArrayList<Item> record = new ArrayList<Item>();

    public void add(Item item) {
        record.add(item);
    }

    public String list() {
        return this.toString();
    }

    public String find(String targetItemTitle) {
        targetItemTitle = targetItemTitle.toLowerCase();

        String foundItemsString = "ID |  Type  | Details\n";

        Integer idx = 0;
        for (Item item : record) {

            String itemTitle = item.getTitle().toLowerCase();

            if (itemTitle.equals(targetItemTitle)) {
                foundItemsString += itemToString(item, idx);
            }

            idx++;

        }

        return  foundItemsString;
    }

    public Item getItemByID(Integer targetItemID) {
        try {
            return record.get(targetItemID);
        } catch (IndexOutOfBoundsException ex) {
            return null;
        }
    }

    public String toString() {
        String recordString = "ID |  Type  | Details\n";

        Integer idx = 0;
        for (Item item : record) {
            recordString += itemToString(item, idx);
            idx++;
        }

        return recordString;
    }


    private String itemToString(Item item, Integer id) {
        return (id.toString() + "  | " + item.toString() + "\n");
    }

}
